import {ChangeDetectorRef, Component, Inject, Injector, OnInit} from '@angular/core';
import {NzFormatEmitEvent, NzMessageService} from 'ng-zorro-antd';
import {SysConfigService} from '../service/sys-config.service';
import {ControlUIWidget, SFComponent, SFCustomWidgetSchema, SFItemComponent} from '@delon/form';
import {ComboBoxTreeNode} from '@sb/base/core/entity/combo-box-tree-node';


export interface SysConfigPageTreeSelectAlainSchema extends SFCustomWidgetSchema {
    /**
     * 页面标识
     */
    pageId?: string;

    ignoreId?: string;
}


@Component({
    selector: 'sys-config-page-tree-select-alain',
    templateUrl: './sys-config-page-tree-select-alain.component.html',
    styleUrls: ['./sys-config-page-tree-select-alain.component.less']
})
export class SysConfigPageTreeSelectAlainComponent extends ControlUIWidget<SysConfigPageTreeSelectAlainSchema> implements OnInit {
    get selectedValue(): any {
        return this._selectedValue;
    }

    set selectedValue(value: any) {
        this._selectedValue = value;

        this.setValue(value);
    }

    /* 用于注册小部件 KEY 值 */
    static readonly KEY = 'sys-config-page-tree-select-alain';

    private _selectedValue: any;

    nzDropdownStyle = {
        width: '300px',
        'overflow-x': 'hidden',
        'min-height': '200px'
    };

    codeValues: ComboBoxTreeNode[] = [];

    pageId = '';


    constructor(
        protected messageService: NzMessageService,
        protected sysConfigService: SysConfigService,
        @Inject(ChangeDetectorRef) public readonly cd: ChangeDetectorRef,
        @Inject(Injector) public readonly injector: Injector,
        @Inject(SFItemComponent) public readonly sfItemComp?: SFItemComponent,
        @Inject(SFComponent) public readonly sfComp?: SFComponent
    ) {
        super(cd, injector, sfItemComp, sfComp);
    }


    ngOnInit(): void {
        this.updateCodes(this.ui.pageId);
    }

    reset(_value: any): void {
        this._selectedValue = _value;
    }

    updateCodes(pageId?) {
        if (pageId !== undefined) {
            this.pageId = pageId;
        }
        if (!this.pageId) {
            this.codeValues = [];
            this.cd.detectChanges();
            return;
        }
        this.sysConfigService.getComboBoxNzTree(this.pageId).subscribe(returnForm => {
            if (returnForm.success) {
                if (this.ui.ignoreId) {
                    returnForm.message.forEach(node => {
                        const children = node.children;
                        for (let i = 0; i < children.length; i++) {
                            if (children[i].key === this.ui.ignoreId) {
                                children.splice(i, 1);
                            }
                        }
                    });
                }
                this.codeValues = returnForm.message;
                this.cd.detectChanges();
            } else {
                this.messageService.error('加载节点数据异常：' + returnForm.errorMessage);
            }
        });
    }

    onExpandChange(e: NzFormatEmitEvent) {
        const node = e.node;
        if (node && node.getChildren().length === 0 && node.isExpanded) {
            this.updateCodes();
        }
    }
}
